package main;

import java.util.Date;

import javax.swing.JSlider;


public class TimeLineSlider extends JSlider {

    /**
	 * 
	 */
	private static final long serialVersionUID = 8942286590518918775L;
	/**
     * Constructs a RangeSlider with default minimum and maximum values of 0
     * and 100.
     */
	public int trackHeight = 10;
	public int minSlider;
	public int maxSlider;
	public int lowerValue;
	public int currentValue;
	public int upperValue;
	
	public long beginTime;
	
    public TimeLineSlider() {
        initSlider();
    }

    /**
     * Constructs a RangeSlider with the specified default minimum and maximum 
     * values.
     */
    public TimeLineSlider(long beginTime, long endTime) {
        super(0, (int)(endTime-beginTime));
        
        this.beginTime = beginTime;
        this.minSlider = 0;
        this.maxSlider = (int) (endTime-beginTime);
        this.setLowerValue(0);
        this.setValue(0);
        this.setUpperValue((int) (endTime-beginTime));
        
        initSlider();
    }
    
    public long getBeginTime()
    {
    	return this.beginTime;
    }

    /**
     * Initializes the slider by setting default properties.
     */
    private void initSlider() {
        setOrientation(HORIZONTAL);
    }

    /**
     * Overrides the superclass method to install the UI delegate to draw two
     * thumbs.
     */
    @Override
    public void updateUI() {
        setUI(new TimeLineSliderUI(this));
        // Update UI for slider labels.  This must be called after updating the
        // UI of the slider.  Refer to JSlider.updateUI().
        updateLabelUIs();
    }

    /**
     * Returns the lower value in the range.
     */
    @Override
    public int getValue() {
        return super.getValue();
    }

    /**
     * Sets the lower value in the range.
     */
    @Override
    public void setValue(int value)
    {        
    	//super.setValue(value);
        this.currentValue = value;

        // Set new value and extent, and fire a single change event.
        getModel().setRangeProperties(this.minSlider, this.maxSlider - this.minSlider, this.minSlider, 
       this.maxSlider, getValueIsAdjusting());
    }

    /**
     * Returns the upper value in the range.
     */
    public int getLowerValue() {
        return lowerValue;
    }

    /**
     * Sets the upper value in the range.
     */
    public void setLowerValue(int value) {
    	if(value <= upperValue)
    	{
    		this.lowerValue = value;
    	}
    	else
    	{
    		this.lowerValue = upperValue;
    	}
    }
    
    /**
     * Returns the upper value in the range.
     */
    public int getUpperValue() {
        return upperValue;
    }

    /**
     * Sets the upper value in the range.
     */
    public void setUpperValue(int value) {
    	if(value >= lowerValue)
    	{
    		this.upperValue = value;
    	}
    	else
    	{
    		this.upperValue = lowerValue;
    	}
    }

	public void repaintCurrentThumb()
	{
		this.updateUI();
		//((TimeLineSliderUI)this.getUI()).listener.moveCurrentThumb();
	}

	public boolean isDragging()
	{
		return (currentIsDragging() || lowerIsDragging() || upperIsDragging());
	}
	
	public boolean currentIsDragging() {
		return ((TimeLineSliderUI)this.getUI()).currentDragging;
	}
	
	public boolean lowerIsDragging() {
		return ((TimeLineSliderUI)this.getUI()).lowerDragging;
	}
	
	public boolean upperIsDragging() {
		return ((TimeLineSliderUI)this.getUI()).upperDragging;
	}
}